package com.wanmait.schedule.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.*;
import com.wanmait.schedule.interceptor.WebSocketServer;
import com.wanmait.schedule.service.*;
import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * <p>
 * 通知 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RequestMapping("/manage/notice")
@RestController
@RequiresPermissions("manage:notice")//没有权限会抛出异常
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Autowired
    private NoticeLevelService noticeLevelService;

    @Autowired
    private NoticeTypeService noticeTypeService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Resource
    private WebSocketServer webSocketServer;

    //获取教师
    @GetMapping("findByTeacher")
    public AjaxResult noticeTeacherList(){
        List<Teacher> teacherList = teacherService.findList();
        return AjaxResult.success("成功").put("teacherList",teacherList);
    }

    /**
     * 根据班级id查询学生
     * @param id 班级id
     * @return
     */
    @GetMapping("findByClassStudent/{id}")
    public AjaxResult findByClassStudent(@PathVariable Integer id){
        List<Student> studentList = studentService.findByClassStudent(id);
        return AjaxResult.success("成功").put("studentList",studentList);
    }

    /**
     * 后台通知页面 获得还没有毕业的班级
     * @return
     * @param id 班级类型id
     */
    @GetMapping("noticeFindList/{id}")
    public AjaxResult findNotGraduation(@PathVariable("id") Integer id){
        List<Classes> classesList = classesService.findNotGraduation(id);
        return AjaxResult.success("成功").put("classesList",classesList);
    }


    //添加通知 或 修改通知
    @PostMapping("/save")
    public AjaxResult noticeSave(@RequestBody Map map) throws IOException {
        return noticeService.save(map)?AjaxResult.success("成功"):AjaxResult.error(10001,"失败");
    }


    //删除
    @GetMapping("/delete")
    public AjaxResult noticeDelete(Integer id){
        boolean remove = noticeService.removeById(id);
        if(remove){
            return AjaxResult.success("成功");
        }
        return AjaxResult.error(10001,"失败");
    }

    //列表
    @PostMapping("/list")

    public AjaxResult noticeList(@RequestBody Notice notice,Integer pageNum){
        List<NoticeType> noticeTypeList = noticeTypeService.list();
        List<NoticeLevel> noticeLevelList = noticeLevelService.list();
        PageInfo<Notice> noticeList = noticeService.findAll(notice, pageNum);
        return AjaxResult.success("成功")
                .put("noticeList",noticeList)
                .put("noticeTypeList",noticeTypeList)
                .put("noticeLevelList",noticeLevelList);
    }

    //根据id获得通知
    @GetMapping("/findById/{id}")
    public AjaxResult findById(@PathVariable("id") Integer id){
        return AjaxResult.success(noticeService.getById(id));
    }

    //通知总数
    @GetMapping("total")
    public AjaxResult total(){
        return AjaxResult.success("成功",noticeService.count());
    }

    //根据id修改
    @PostMapping("updataById")
    public AjaxResult updataById(@RequestBody Notice notice){

        boolean flag = noticeService.updateById(notice);

        return flag?AjaxResult.success("成功"):AjaxResult.error(1001,"失败");
    }

    //根据通知Id获得人员
    @GetMapping("findIdByStudent")
    public AjaxResult findIdByStudent(Integer id,Integer pageNum){ return AjaxResult.success("成功",noticeService.findIdByStudent(id,pageNum)); }

    //根据通知Id获得人员
    @GetMapping("findIdByTeacher")
    public AjaxResult findIdByTeacher(Integer id,Integer pageNum){ return AjaxResult.success("成功",noticeService.findIdByTeacher(id, pageNum)); }

    //根据通知Id 学生Id删除 通知消息
    @PostMapping("delNoticeStudent")
    public AjaxResult delNoticeStudent(@RequestBody NoticeRecord noticeRecord){
        Boolean aBoolean = noticeService.delNoticeStudent(noticeRecord);
        return aBoolean?AjaxResult.success("成功"):AjaxResult.error(10002,"失败");
    }
    //根据通知Id 教师Id删除 通知消息
    @PostMapping("delNoticetTeacher")
    public AjaxResult delNoticeTeacher(@RequestBody NoticeRecord noticeRecord){
        Boolean aBoolean = noticeService.delNoticeTeacher(noticeRecord);
        return aBoolean?AjaxResult.success("成功"):AjaxResult.error(10002,"失败");
    }


}
